class Admins::WeibosController < ApplicationController
  before_action :set_weibo, only: [:show, :edit, :update, :destroy,:submit,:approve,:reject]

  def index
    weibo_type = params[:weibo_type] || '1'
    @current_user = current_user
    @weibos = Weibo.weibo_type(weibo_type,current_user).check(params[:check]).page(params[:page])
  end

  def show
  end

  def new
    if params[:weibo].blank?
      @weibo = Weibo.new
    else
      @weibo = Weibo.new(weibo_params)
    end
  end

  def edit
  end

  def create
   
    @weibo = Weibo.new(weibo_params.merge({postby: current_user.name}))
    @weibo.contribute_unit_name  = Department.find(current_user.department_id).name
    if @weibo.save
      redirect_to admins_weibos_url, notice: '微博创建成功.'
    else
      flash[:error] = @weibo.errors.full_messages.join(",")
      redirect_to :action => :new,:weibo => @weibo.attributes
    end
  end

  def submit
      if @weibo.update_attributes(check: 1)
        redirect_to admins_weibos_path @weibo, notice: '微博送审成功.'
      else
        render :edit
      end
  end

  #采用
  def approve
    @weibo.update_attributes(check: 2)
    redirect_to admins_weibos_path @weibo, notice: '微博采用成功.'
  end

  #不采用
  def reject
    @weibo.update_attributes(check: 3,reason: params[:reason])
    redirect_to admins_weibos_path @weibo, notice: '微博退回成功.'
  end

  # PATCH/PUT /weibos/1
  # PATCH/PUT /weibos/1.json
  def update
    respond_to do |format|
      if @weibo.update(weibo_params)
        format.html { redirect_to admins_weibos_url, notice: '微博修改成功.' }
        format.json { render :show, status: :ok, location: @weibo }
      else
        format.html { render :edit }
        format.json { render json: @weibo.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /weibos/1
  # DELETE /weibos/1.json
  def destroy
    @weibo.destroy
    respond_to do |format|
      format.html { redirect_to admins_weibos_url, notice: 'Weibo was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_weibo
      @weibo = Weibo.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def weibo_params
      params.require(:weibo).permit(:author, :contribute_unit_name, :phone, :content, :long_content)
    end
end
